﻿using NB.MGJ.Common.Cache;
using NB.MGJ.Model;
using NB.MGJ.Service;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public class MobileHomeController : Controller
    {
        private HomeService _HomeService = new HomeService();
        public ActionResult LoginIndex(int batchId)
        {
            ViewBag.batchId = batchId;
            return View(JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 登录接口
        /// </summary>
        /// <param name="model">登录用户模型</param>
        /// <returns>ht</returns>
        public ActionResult Login(LogOnModel model)
        {
            Hashtable ht = new Hashtable();
            if (string.IsNullOrWhiteSpace(model.UserName))
            {
                ht.Add("Msg", "登录名不能为空");
                ht.Add("IsLogin", false);
                return Json(ht);
            }
            if (string.IsNullOrWhiteSpace(model.Password))
            {
                ht.Add("Msg", "密码不能为空");
                ht.Add("IsLogin", false);
                return Json(ht);
            }
            model.UserName = model.UserName.Trim();
            model.Password = model.Password.Trim();
            int? userid = null;
            if (!_HomeService.UserLogin(model.UserName, model.Password, out userid))
            {
                ht.Add("Msg", "用户名密码不正确");
                ht.Add("IsLogin", false);
                return Json(ht);
            }
            var _user = _HomeService.GetUserByUserId(userid.Value);
            if (_user == null)
            {
                ht.Add("Msg", "用户名数据信息丢失不存在");
                ht.Add("IsLogin", false);
                return Json(ht);
            }
            UserHelper.SetUserToSession(_user);
            ht.Add("Msg", "登录成功");
            ht.Add("IsLogin", true);
            return Json(ht);
        }
	}
}